package com.ctshk.rpc.starvip.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.HttpUtils;
import com.ctshk.rpc.starvip.dto.start_vip.EmptySiebelMessage;
import com.ctshk.rpc.starvip.dto.start_vip.member.CtsMemberExpirationPoint;
import com.ctshk.rpc.starvip.dto.start_vip.member.MemberExpiredPointsQueryDTO;
import com.ctshk.rpc.starvip.dto.start_vip.member.UpdateMemberTierSiebelMessage;
import com.ctshk.rpc.starvip.dto.start_vip.user.Contact;
import com.ctshk.rpc.starvip.dto.start_vip.user.UserInfo;
import com.ctshk.rpc.starvip.factory.member.*;
import com.ctshk.rpc.starvip.req.member.*;
import com.ctshk.rpc.starvip.service.IStarVipMemberService;
import com.ctshk.rpc.starvip.util.JaxbUtil;
import com.ctshk.rpc.starvip.util.ReqUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.rpc.starvip.service.impl
 * @Author: hef
 * @CreateTime: 2021-04-12 17:31
 * @Description: 描述
 */
@DubboService
public class StarVipMemberServiceImpl implements IStarVipMemberService {


    @Override
    public Result<Contact> membershipVerification(MembershipVerificationReq req) {
        MembershipVerificationFactory factory = new MembershipVerificationFactory(req);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(factory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(factory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type","application/json");
            result = HttpUtils.postRequest("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage,header, response -> {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(factory);
        UserInfo userInfo = ReqUtil.fromXml(UserInfo.class, result);
        if (Objects.nonNull(userInfo) &&
                Objects.nonNull(userInfo.getErrorInfo()) &&
                Objects.nonNull(userInfo.getErrorInfo().getErrorCode())) {
            throw new BusinessException(SystemError.STAR_VIP_20000, userInfo.getErrorInfo().getErrorMessage());
        }
        if(Objects.isNull(userInfo)
                || Objects.isNull(userInfo.getListOfContact() )
                || CollectionUtils.isEmpty(userInfo.getListOfContact().getContact())){
            throw new BusinessException(SystemError.STAR_VIP_20003);
        }
        //返回会员用户信息
        return Result.success(userInfo.getListOfContact().getContact().get(0));
    }

    @Override
    public Result resetPassword(MemberResetPasswordReq req) {
        ResetPasswordFactory resetPasswordFactory = new ResetPasswordFactory(req);
        String result = ReqUtil.sendReq(resetPasswordFactory);
        EmptySiebelMessage errorMessage = ReqUtil.fromXml(EmptySiebelMessage.class, result);
        if (Objects.isNull(errorMessage) ||
                !errorMessage.checkErrorCode(604)) {
            throw new BusinessException(SystemError.STAR_VIP_20001);
        }
        return Result.success();
    }

    @Override
    public Result changePassword(MemberChangePasswordReq req) {
        MemberChangePasswordFactory factory = new MemberChangePasswordFactory(req);
        String result = ReqUtil.sendReq(factory);
        EmptySiebelMessage errorMessage = ReqUtil.fromXml(EmptySiebelMessage.class, result);
        if (Objects.isNull(errorMessage) || !errorMessage.checkErrorCode(605))
            throw new BusinessException(SystemError.STAR_VIP_20002);
        return Result.success();
    }

    @Override
    public Result<CtsMemberExpirationPoint> memberExpiredPointsQuery(MemberExpiredPointsQueryReq req) {
        MemberExpiredPointsQueryFactory factory = new MemberExpiredPointsQueryFactory(req);
        String result = ReqUtil.sendReq(factory);
        MemberExpiredPointsQueryDTO point = ReqUtil.fromXml(MemberExpiredPointsQueryDTO.class, result);
        //成功的时候会返回对应信息
        if (Objects.nonNull(point)
                && Objects.nonNull(point.getListOfMemberExprPointsItem())
                && CollectionUtils.isNotEmpty(point.getListOfMemberExprPointsItem().getCtsMemberExpirationPoint())) {
            return Result.success(point.getListOfMemberExprPointsItem().getCtsMemberExpirationPoint().get(0));
        }
        if (Objects.nonNull(point.getErrorInfo())) {
            throw new BusinessException(SystemError.STAR_VIP_20000, point.getErrorInfo().getErrorMessage());
        }
        return Result.failed(SystemError.SYS_437);
    }

    @Override
    public Result updateTier(@Valid UpdateMemberTierReq req) {
        UpdateMemberTierFactory factory = new UpdateMemberTierFactory(req);
        String result = ReqUtil.sendReq(factory);
        UpdateMemberTierSiebelMessage dto = ReqUtil.fromXml(UpdateMemberTierSiebelMessage.class, result);
        //成功的时候会返回对应信息
        if (Objects.nonNull(dto)
                && Objects.nonNull(dto.getUpdateMemberTier())
                && Objects.nonNull(dto.getUpdateMemberTier().getLoyMember())) {
            return Result.success(dto.getUpdateMemberTier().getLoyMember());
        }
        if (Objects.nonNull(dto.getErrorInfo())) {
            throw new BusinessException(SystemError.STAR_VIP_20000, dto.getErrorInfo().getErrorMessage());
        }
        return Result.failed(SystemError.SYS_437);
    }

}
